package com.ssm.student.controller;

import com.ssm.student.eo.TestPaperEoPage;
import com.ssm.student.model.TestPaper;
import com.ssm.student.service.TestPaperService;
import com.ssm.student.tool.ResponseMessage;
import com.ssm.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "testpaper")
@RestController
public class TestPaperController {
    @Autowired
    private TestPaperService testPaperService;
    @ApiOperation(value = "试卷添加方法")
    @RequestMapping(value = "/addTestPaper",method = RequestMethod.GET )
    public ResponseMessage add(TestPaper testpaper) {
        int IsAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (testpaper != null) {
            //(2)角色名称不能为空
            if (testpaper.getPapername()!= null) {
                //(3)添加角色数据
                IsAdd = testPaperService.AddTestPaper(testpaper);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("试卷重复，不能添加");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "试卷删除方法")
    @RequestMapping(value = "/deleteTestPaper",method = RequestMethod.GET )
    public ResponseMessage deleteTestPaper(int testpaperid) {
        if (testpaperid != 0) {
            int delInt = 0;
            delInt = testPaperService.DelTestPaper(testpaperid);
            if (delInt > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } else {
            return Result.error("删除的主键不能为0请重新输入");
        }
    }
    @ApiOperation(value = "试卷修改方法")
    @RequestMapping(value = "/updateTestPaper",method = RequestMethod.GET )
    public ResponseMessage uedateTestPaper(TestPaper testPaper) {
        //传入的参数不能为空
        if (testPaper != null) {
            //参数中主键不能为0
            if (testPaper.getTestpaperid() != 0) {
                if (testPaperService.UpdateTestPaper(testPaper) == 1) {
                    //
                    if (testPaper.getPapername()!=null){
                        if (testPaper.getClassid().length()<5){
                            return Result.success("更新成功");
                        }else {
                            return Result.error("更新失败");
                        }
                    }else {
                        return Result.error("更新失败");
                    }

                } else {
                    return Result.error("更新失败");
                }
            } else {
                return Result.error("更新传入参数不正确");
            }
        }

        return Result.error("更新传入参数不正确");
    }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getAllTestPaper",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    private  ResponseMessage getAllTestPaper(@RequestBody TestPaperEoPage testPaperEoPage){
        Map<String, List> map=new HashMap<>();
        List<TestPaperEoPage> listtestPape=testPaperService.getWhereAll(testPaperEoPage);
        map.put("list",listtestPape);
        return Result.success("200",map);
    }
}